package com.hrms.hrms_recruitment.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hrms.common.vo.ResultMessage;
import com.hrms.hrms_recruitment.dao.RecruitDao;
import com.hrms.hrms_recruitment.entity.JobPostings;
import com.hrms.hrms_recruitment.entity.Recruit;
import com.hrms.hrms_recruitment.entity.RecruitDetail;
import com.hrms.hrms_recruitment.entity.StaffPosition;
import com.hrms.hrms_recruitment.service.*;
import com.hrms.hrms_recruitment.vo.RecruitVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.concurrent.ThreadLocalRandom;

/**
 * (Recruit)表服务实现类
 *
 * @author makejava
 * @since 2025-01-14 16:11:59
 */
@Service("recruitService")
public class RecruitServiceImpl extends ServiceImpl<RecruitDao, Recruit> implements RecruitService {

    @Autowired
    private RecruitClient recruitClient;
    @Autowired
    private EmployeeClient employeeClient;
    @Autowired
    private RecruitDetailService recruitDetailService;
    @Autowired
    private RecruitDao recruitDao;
    @Autowired
    private JobPostingsService jobPostingsService;
    /**
     * 保存并开启招聘流程
     * @param recruitvo
     * @return
     */
    @Override
    @Transactional
    public Boolean saveAndStartFlow(RecruitVO recruitvo) {
        //分配hr stationId 14
        ResultMessage resultMessage = employeeClient.selectDepartmentStaffs(14L);
        Object data = resultMessage.getData();
        // 将list中的数据转成json字符串
        String jsonObject=JSON.toJSONString(data);
        //将json转成需要的对象
        List<StaffPosition> staffPositions = JSON.parseArray(jsonObject, StaffPosition.class);
        StaffPosition staffPosition = staffPositions.get(ThreadLocalRandom.current().nextInt(staffPositions.size()));
        recruitvo.setHr(staffPosition.getName());

        RecruitVO recruitVO = this.saveInfo(recruitvo);
        //获取流程变量
        HashMap<String,Object> variables = this.getVariables(recruitVO,recruitVO.getRecruitDetail());
        ResultMessage msg = recruitClient.recruitStart(variables);
        String insId = (String)msg.getData();
        //保存数据
        recruitVO.setProInsId(insId);
        this.update(new UpdateWrapper<Recruit>().set("pro_ins_id",insId).eq("id",recruitVO.getId()));
        return msg.getResult() == 200 ? true : false;
    }

    /**
     * 获取所有的招聘需求列表
     * @param page
     * @param map
     * @return
     */
    @Override
    public Page<RecruitVO> pageInfo(Page<RecruitVO> page, Map<String, String> map) {
        Page<RecruitVO> recruitVOPage = this.recruitDao.pageInfo(page,map);
        return recruitVOPage;
    }

    /**
     * 通过主键查询信息
     * @param id
     * @return
     */
    @Override
    public RecruitVO getDetailsById(Long id) {
        RecruitVO recruitVO = new RecruitVO();
        Recruit recruit = this.getById(id);
        RecruitDetail detail = this.recruitDetailService.getOne(new QueryWrapper<RecruitDetail>().eq("recruit_id", id));
        BeanUtils.copyProperties(recruit,recruitVO);
        recruitVO.setRecruitDetail(detail);
        return recruitVO;
    }

    /**
     * 招聘审批流程结束修改状态
     */
    @Override
    public void updateByDetails(String id) {
        this.update(new UpdateWrapper<Recruit>().set("state",2).eq("id",Long.parseLong(id)));
    }
    /**
     * 面试审批流程结束修改状态并且新增账号
     */
    @Override
    public void updateInterViewByDetails(Long id) {
        //主表
        this.update(new UpdateWrapper<Recruit>().set("state",4).eq("id",id));
        //job表
        this.jobPostingsService.update(new UpdateWrapper<JobPostings>().eq("recruit_id",id).set("state",1));
    }

    /**
     * 获取流程变量
     * @return
     */
    private HashMap<String, Object> getVariables(RecruitVO recruitvo, RecruitDetail recruitDetail) {
        HashMap<String, Object> map = new HashMap<>();
        //hr
        map.put("hr",recruitvo.getHr());
        //获取所在部门总经理总经理
        map.put("manager","lisi");
        //面试
        map.put("interviewer",recruitDetail.getInterviewer());
        map.put("reInterviewer",recruitDetail.getReInterviewer());
        //财务部门审批
        map.put("compensation","wangwu");
        //提交人
        map.put("director",recruitvo.getName());
        map.put("recruitvo",recruitvo.getId());
        //需求等级
        map.put("level",recruitvo.getLevel());

        //启动人信息
        map.put("initiator",recruitvo.getName());
        return map;
    }

    /**
     * 保存数据
     * @param recruitvo
     */
    private RecruitVO saveInfo(RecruitVO recruitvo) {
        recruitvo.setState(1);
        Recruit recruit = new Recruit();
        BeanUtils.copyProperties(recruitvo,recruit);
        this.baseMapper.insert(recruit);
        recruitvo.setId(recruit.getId());
        RecruitDetail recruitDetail = recruitvo.getRecruitDetail();
        recruitDetail.setRecruitId(recruit.getId());
        recruitDetailService.save(recruitDetail);
        recruitvo.getRecruitDetail().setId(recruitDetail.getId());
        return recruitvo;
    }
}

